// --------------------------------------------------------------------------------------------------------------------
// <copyright file="KinectObjectDetectorIR.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Robotics.Vision.ObjectDetection
{
    using System;
    using System.Collections.Generic;
    using System.Windows;

    using Microsoft.Robotics.Vision;

    /// <summary>
    /// Class for object detection using IR plus depth image from a kinect
    /// </summary>
    public class KinectObjectDetectorIR
    {
        /// <summary>
        /// Object predictor
        /// </summary>
        private readonly ObjectPosePredictor predictor;

        /// <summary>
        /// Expected object width in MM
        /// </summary>
        private readonly int objectWidthInMM;

        /// <summary>
        /// Expected object Height in MM
        /// </summary>
        private readonly int objectHeightInMM;

        /// <summary>
        /// Level 1 threshold
        /// </summary>
        private double level1Threshold;

        /// <summary>
        /// Level 2 threshold
        /// </summary>
        private double level2Threshold;

        /// <summary>
        /// Scaling factor for getting context from from level 2 patch
        /// </summary>
        private double level2PatchScale;

        /// <summary>
        /// Initializes a new instance of the <see cref="KinectObjectDetectorIR"/> class.
        /// </summary>
        /// <param name="fx">Focal length x in mm</param>
        /// <param name="fy">Focal length y in mm</param>
        /// <param name="netL1">Level 1 net file name</param>
        /// <param name="netL2Classify">Level 2 classifier net file name</param>
        /// <param name="netL2Regress">Level 2 regression net file name</param>
        /// <param name="objectWidthInMM">Expected object width in mm</param>
        /// <param name="objectHeightInMM">Expected object height in mm</param>
        /// <param name="level2PatchWidth">Level 2 processing patch width</param>
        /// <param name="level2PatchHeight">Level 2 processing patch height</param>
        /// <param name="level2PatchScale">Scale factor for obtaining context from level 2 patch</param>
        /// <param name="level1Threshold">Level 1 classification threshold</param>
        /// <param name="level2Threshold">Level 2 classification threshold</param>
        public KinectObjectDetectorIR(
            double fx,
            double fy,
            string netL1,
            string netL2Classify,
            string netL2Regress,
            int objectWidthInMM,
            int objectHeightInMM,
            int level2PatchWidth,
            int level2PatchHeight,
            double level2PatchScale,
            double level1Threshold, 
            double level2Threshold)
        {
            this.predictor = new ObjectPosePredictor(fx, fy, netL1, netL2Classify, netL2Regress, new Size(level2PatchWidth, level2PatchHeight));
            this.objectWidthInMM = objectWidthInMM;
            this.objectHeightInMM = objectHeightInMM;
            this.level2PatchScale = level2PatchHeight;
            this.level1Threshold = level1Threshold;
            this.level2Threshold = level2Threshold;
        }

        /// <summary>
        /// Run object detection on an IR and depth frames.
        /// </summary>
        /// <param name="irFrame">Input IR image</param>
        /// <param name="depthFrame">Input depth frame</param>
        /// <returns>Collection of predicted patches</returns>
        public List<PatchCluster> Predict(ImageFrame<ushort> irFrame, ImageFrame<ushort> depthFrame)
        {
            return this.predictor.Predict(irFrame, depthFrame, this.objectWidthInMM, this.objectHeightInMM, this.level2PatchScale, this.level1Threshold, this.level2Threshold);
        }
    }
}
